package http.get;


import annotation.Get;
import annotation.HttpService;
import annotation.Path;
import annotation.Post;
import com.mysql.jdbc.JDBC4Connection;
import com.mysql.jdbc.LoadBalancedAutoCommitInterceptor;
import database.DBManager;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import spark.Request;
import spark.Response;
import spark.Route;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import static spark.Spark.get;
import static spark.Spark.post;


@Path("/g/login")
public class ReqLogin extends BaseRequest {

    @Post
    public Object callAnyRespone(Request request, Response response) {
        return checkLogin(request);
    }

    private  String checkLogin(Request request) {
        String userName = request.queryParams("userName");
        String pwd = request.queryParams("userPwd");
        String token = request.queryParams("md5Token");
        String timestamp = request.queryParams("timeStamp");

        //查询数据库进行登录
        String result = queryDatabase(userName,pwd);
        if (result.length() <= 0) {

            return failedLogin();
        }
        return successRespToClient(result);
    }

    /**
     * 通过用户名和密码查询数据库返回User信息给用户
     * @param userName
     * @param userPwd
     */
    private String queryDatabase(String userName , String userPwd){
        String sql ="select * from user where username=\'" + userName +"\' and userpwd=\'" + userPwd + "\'";
        System.out.println(sql);
        PreparedStatement statement = DBManager.getInstance().getPreStatement(sql);
        try {
            ResultSet set = statement.executeQuery();
            StringBuffer buffer = new StringBuffer();
            System.out.println("-->" + set.getMetaData().getColumnCount());
            if(set.next()) {
                for (int i = 1 ; i < set.getMetaData().getColumnCount()+1; i++){
                    if (i == 1) {
                        buffer.append("{\"" + set.getMetaData().getColumnName(i) + "\":" + "\"" +set.getObject(i) +"\",");
                    } else if (i == set.getMetaData().getColumnCount() ){
                        buffer.append("\"" + set.getMetaData().getColumnName(i) + "\":" + "\"" +set.getObject(i) +"\"}");
                    } else {
                        buffer.append("\"" + set.getMetaData().getColumnName(i) + "\":" + "\"" +set.getObject(i) +"\",");
                    }
                }
            }
            return buffer.toString();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private  String successRespToClient(String userJsonData){
        String userCreateTime = "{\"code\":\"200\",\"loginTime\":\""+System.currentTimeMillis()+"\",\"user\":" + userJsonData + "}";
        System.out.println("" + userCreateTime);
        return userCreateTime;
    }

    private String failedLogin(){
        return "{\"code\":\"500\",\"loginTime\":\""+System.currentTimeMillis()+"\",\"user\":" + null + "}";
    }

}
